package net.a6polo.a6poloweibo;

import java.io.IOException;

import com.google.gson.Gson;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.WeiboAPI;
import com.weibo.sdk.android.net.RequestListener;

import net.a6polo.a6poloweibo.adapter.RepostListViewAdapter;
import net.a6polo.a6poloweibo.sina.domain.Reposts;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView;
import net.a6polo.a6poloweibo.view.MyProgressDialog;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView.OnRefreshListener;
import net.a6polo.service.Service;
import net.a6polo.service.SnsType;
import net.a6polo.service.domain.Account;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class RepostActivity extends Activity {
	
	private Handler handler = null;
	
	private DropDownToRefreshListView repostListView = null;
	
	private static String weiboId;
	
	private StatusesAPI statuesApi;
	
	private Reposts reposts;
	
	private int pageNo = 1;

	private int pageSize = 20;

	private long maxId = 0;
	
	private Exception exception;
	
	private RepostListViewAdapter repostListViewAdapter;
	
	private Dialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repost);
        
        handler = new Handler();
        repostListView = (DropDownToRefreshListView) findViewById(R.id.repostListView);
        progressDialog = new MyProgressDialog(this, R.style.CustomProgressDialog);
        
        Intent intent = getIntent();
		weiboId = intent.getStringExtra("weiboId");
		Service service = new Service(RepostActivity.this);
		Account sinaAccount = service.getLastAccountByType(SnsType.SINA_WEIBO);
		Oauth2AccessToken token = new Oauth2AccessToken(sinaAccount.getAccessToken(), sinaAccount.getExpiresIn());
		
		statuesApi = new StatusesAPI(token);
		
		if (null == reposts || reposts.getReposts().size() == 0) {
			progressDialog.show();
			reRefresh();
		}
		
		repostListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				reRefresh();
			}
		});
		
		repostListView.setOnLoadMoreListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				more();
			}
		});
    }
    
    private void reRefresh(){
    	maxId = 0;
    	statuesApi.repostTimeline(Long.parseLong(weiboId), (long) 0, (long) maxId, pageSize, pageNo, WeiboAPI.AUTHOR_FILTER.ALL,
				new RepostRefreshRequestListener());
    }
    
    class RepostRefreshRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			progressDialog.dismiss();
			Gson gson = new Gson();
			Log.d("a6polo", "repost response json:" + json);
			reposts = gson.fromJson(json, Reposts.class);
			
			int maxIndex = reposts.getReposts().size();
			if(maxIndex > 0) {
				maxIndex = maxIndex -1;
				maxId = reposts.getReposts().get(maxIndex).getId();
			}

			new Thread() {
				public void run() {
					handler.post(runnableRefreshUi);
				}
			}.start();

		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

	}
    
    Runnable runnableRefreshUi = new Runnable() {

		@Override
		public void run() {
			loadRepostData(reposts);
		}
	};
	
	private void loadRepostData(Reposts reposts) {
		repostListViewAdapter = new RepostListViewAdapter(reposts, this);
		repostListView.setAdapter(repostListViewAdapter);
	}
	
	private void more() {		
		statuesApi.repostTimeline(Long.parseLong(weiboId), (long) 0, (long) maxId, pageSize, pageNo, WeiboAPI.AUTHOR_FILTER.ALL,
				new RepostMoreRequestListener());
	}
	
	class RepostMoreRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			Gson gson = new Gson();
			reposts = gson.fromJson(json, Reposts.class);
			
			int maxIndex = reposts.getReposts().size();
			if(maxIndex > 0) {
				maxIndex = maxIndex -1;
			}
			maxId = reposts.getReposts().get(maxIndex).getId();

			new Thread() {
				public void run() {
					handler.post(runnableMoreUi);
				}
			}.start();

		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

	}
	
	Runnable runnableMoreUi = new Runnable() {
		@Override
		public void run() {
			moreRepostData(reposts);
		}

	};
	
	private void moreRepostData(Reposts reposts) {
		repostListViewAdapter.addItem(reposts);		
		repostListViewAdapter.notifyDataSetChanged();
	}

}
